using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

using FarseerGames.FarseerPhysicsDemos.DrawingSystem;

using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using FarseerGames.FarseerPhysics;

namespace FarseerGames.FarseerPhysicsDemos.Demos.Demo2
{    public class Ragdoll
    {
        Texture2D rightShoulderTexture, rightForearmTexture, leftShoulderTexture, leftForearmTexture;
        Texture2D rightThighTexture, rightCalfTexture, leftThighTexture, leftCalfTexture;
        Texture2D headTexture;
        Texture2D rectangleTexture;

        Vector2 rightShoulderOrigin, rightForearmOrigin, leftShoulderOrigin, leftForearmOrigin;
        Vector2 rightThighOrigin, rightCalfOrigin, leftThighOrigin, leftCalfOrigin;
        Vector2 headOrigin;
        Vector2 rectangleOrigin;

        Body rightShoulderBody, rightForearmBody, leftShoulderBody, leftForearmBody;
        Body rightThighBody, rightCalfBody, leftThighBody, leftCalfBody;
        Body headBody;
        Body rectangleBody;

        PinJoint rightShoulderJoint, rightElbowJoint, leftShoulderJoint, leftElbowJoint;
        PinJoint rightHipJoint, rightKneeJoint, leftHipJoint, leftKneeJoint;
        PinJoint neckJoint;

        Geom rightShoulderGeom, rightForearmGeom, leftShoulderGeom, leftForearmGeom;
        Geom rightThighGeom, rightCalfGeom, leftThighGeom, leftCalfGeom;
        Geom headGeom;
        Geom rectangleGeom;

        public void Load(GraphicsDevice graphicsDevice, PhysicsSimulator physicsSimulator)
        {
            rightShoulderTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 100, 15, Color.White, Color.Black);
            rightShoulderOrigin = new Vector2(rightShoulderTexture.Width / 2f, rightShoulderTexture.Height / 2f);

            leftShoulderTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 100, 15, Color.White, Color.Black);
            leftShoulderOrigin = new Vector2(leftShoulderTexture.Width / 2f, leftShoulderTexture.Height / 2f);

            rightForearmTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 90, 15, Color.White, Color.Black);
            rightForearmOrigin = new Vector2(rightForearmTexture.Width / 2f, rightForearmTexture.Height / 2f);

            leftForearmTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 90, 15, Color.White, Color.Black);
            leftForearmOrigin = new Vector2(leftForearmTexture.Width / 2f, leftForearmTexture.Height / 2f);

            rightThighTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 25, 70, Color.White, Color.Black);
            rightThighOrigin = new Vector2(rightThighTexture.Width / 2f, rightThighTexture.Height / 2f);

            leftThighTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 25, 70, Color.White, Color.Black);
            leftThighOrigin = new Vector2(leftThighTexture.Width / 2f, leftThighTexture.Height / 2f);

            rightCalfTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 25, 70, Color.White, Color.Black);
            rightCalfOrigin = new Vector2(rightCalfTexture.Width / 2f, rightCalfTexture.Height / 2f);

            leftCalfTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, 25, 70, Color.White, Color.Black);
            leftCalfOrigin = new Vector2(leftCalfTexture.Width / 2f, leftCalfTexture.Height / 2f);

            headTexture = DrawingHelper.CreateCircleTexture(graphicsDevice, 30, Color.Gold, Color.Black);
            headOrigin = new Vector2(headTexture.Width / 2f, headTexture.Height / 2f);

            rightShoulderBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 100, 15, 1);
            rightShoulderBody.Position = new Vector2(345, 120);
            rightShoulderGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightShoulderBody, 100, 15);

            leftShoulderBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 100, 15, 1);
            leftShoulderBody.Position = new Vector2(170, 120);
            leftShoulderGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftShoulderBody, 100, 15);

            rightForearmBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 90, 15, 1);
            rightForearmBody.Position = new Vector2(450, 120);
            rightForearmGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightForearmBody, 90, 15);

            leftForearmBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 90, 15, 1);
            leftForearmBody.Position = new Vector2(65, 120);
            leftForearmGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftForearmBody, 90, 15);

            rightThighBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            rightThighBody.Position = new Vector2(285, 330);
            rightThighGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightThighBody, 25, 70);

            leftThighBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            leftThighBody.Position = new Vector2(225, 330);
            leftThighGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftThighBody, 25, 70);

            rightCalfBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            rightCalfBody.Position = new Vector2(285, 410);
            rightCalfGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, rightCalfBody, 25, 70);

            leftCalfBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 25, 70, 1);
            leftCalfBody.Position = new Vector2(225, 410);
            leftCalfGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, leftCalfBody, 25, 70);

            headBody = BodyFactory.Instance.CreateCircleBody(physicsSimulator, 64, 20);
            headBody.Position = new Vector2(257, 80);
            headGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, headBody, 20, 5);

            rightShoulderJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(rectangleTexture.Width / 2f, -rectangleTexture.Height / 2f), rightShoulderBody, new Vector2(-rightShoulderTexture.Width / 2f, 0));
            leftShoulderJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(-rectangleTexture.Width / 2f, -rectangleTexture.Height / 2f), leftShoulderBody, new Vector2(leftShoulderTexture.Width / 2f, 0));

            rightElbowJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rightShoulderBody, new Vector2(rightShoulderTexture.Width / 2f, 0), rightForearmBody, new Vector2(-rightForearmTexture.Width / 2f, 0));
            leftElbowJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, leftShoulderBody, new Vector2(-leftShoulderTexture.Width / 2f, 0), leftForearmBody, new Vector2(leftForearmTexture.Width / 2f, 0));

            rightHipJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(rectangleTexture.Width / 2f, rectangleTexture.Height / 2f), rightThighBody, new Vector2(0, -rightThighTexture.Height / 2f));
            leftHipJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(-rectangleTexture.Width / 2f, rectangleTexture.Height / 2f), leftThighBody, new Vector2(0, -leftThighTexture.Height / 2f));

            rightKneeJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rightThighBody, new Vector2(0, rightThighTexture.Height / 2f), rightCalfBody, new Vector2(0, -rightCalfTexture.Height / 2f));
            leftKneeJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, leftThighBody, new Vector2(0, leftThighTexture.Height / 2f), leftCalfBody, new Vector2(0, -leftCalfTexture.Height / 2f));

            neckJoint = JointFactory.Instance.CreatePinJoint(physicsSimulator, rectangleBody, new Vector2(0, -rectangleTexture.Height / 2f), headBody, new Vector2(0, headTexture.Height / 2f));

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(rightShoulderTexture, rightShoulderGeom.Position, null, Color.White, rightShoulderGeom.Rotation, rightShoulderOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(leftShoulderTexture, leftShoulderGeom.Position, null, Color.White, leftShoulderGeom.Rotation, leftShoulderOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(rightForearmTexture, rightForearmGeom.Position, null, Color.White, rightForearmGeom.Rotation, rightForearmOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(leftForearmTexture, leftForearmGeom.Position, null, Color.White, leftForearmGeom.Rotation, leftForearmOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(rightThighTexture, rightThighGeom.Position, null, Color.White, rightThighGeom.Rotation, rightThighOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(leftThighTexture, leftThighGeom.Position, null, Color.White, leftThighGeom.Rotation, leftThighOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(rightCalfTexture, rightCalfGeom.Position, null, Color.White, rightCalfGeom.Rotation, rightCalfOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(leftCalfTexture, leftCalfGeom.Position, null, Color.White, leftCalfGeom.Rotation, leftCalfOrigin, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(headTexture, headGeom.Position, null, Color.White, headGeom.Rotation, headOrigin, 1, SpriteEffects.None, 0);
        }




    }
}